#include <errno.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include "./IPC/SharedMemory/ControlDataSharedMemory.h"
#include "./IPC/Semaphore/Semaphore.h"
#include "./IPC/MessageQueue/DoorInputMessageQueue.h"
#include "./IPC/MessageQueue/DoorOutputMessageQueue.h"
#include "common.h"


int main(int argc, char **argv)
{	
	// Libero la memoria compartida y su semáforo asociado
	Semaphore semControl = Semaphore();
	semControl.getSemaphore((char *)DIRECTORY, ID_SEMCONTROL, 1);
	semControl.destroy();
	
	Semaphore semDoor = Semaphore();
	semDoor.getSemaphore((char *) DIRECTORY, ID_SEMDOOR, 4);
	semDoor.destroy();

	// TODO: Habría que desattachar a todos los procesos que están en la 
	// memoria compartida
	ControlDataSharedMemory shMem = ControlDataSharedMemory();
	shMem.getSharedMemory((char *)DIRECTORY, ID_SHMEM);
	shMem.destroy();
	
	//Libero las colas de mensaje
	DoorInputMessageQueue entryDoorQueueInput = DoorInputMessageQueue();
	entryDoorQueueInput.getMessageQueue((char *) DIRECTORY, ID_Q_ENTRY_DOOR_INPUT);
	entryDoorQueueInput.destroy();
	DoorOutputMessageQueue entryDoorQueueOutput = DoorOutputMessageQueue();
	entryDoorQueueOutput.getMessageQueue((char *) DIRECTORY, ID_Q_ENTRY_DOOR_OUTPUT);
	entryDoorQueueOutput.destroy();
	
	DoorInputMessageQueue exitDoorQueueInput = DoorInputMessageQueue();
	exitDoorQueueInput.getMessageQueue((char *) DIRECTORY, ID_Q_EXIT_DOOR_INPUT);
	exitDoorQueueInput.destroy();
	DoorOutputMessageQueue exitDoorQueueOutput = DoorOutputMessageQueue();
	exitDoorQueueOutput.getMessageQueue((char *) DIRECTORY, ID_Q_EXIT_DOOR_OUTPUT);
	exitDoorQueueOutput.destroy();
	
	return 0;
}

